package com.hzjy.manage.service.impl;

import java.util.Date;
import java.util.List;
import com.hzjy.common.utils.DateUtils;
import com.hzjy.common.utils.SecurityUtils;
import com.hzjy.manage.mapper.AuditDelistMapper;
import com.hzjy.manage.mapper.HzjyDelistMapper;
import com.hzjy.manage.service.AuditDelistService;
import com.hzjy.manage.vo.AuditDelist;
import com.hzjy.manage.vo.AuditRecord;
import com.hzjy.manage.vo.HzjyDelist;
import com.hzjy.manage.vo.HzjyDelistVO;
import com.hzjy.system.domain.SysNotice;
import com.hzjy.system.mapper.SysNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 摘牌审核记录Service业务层处理
 * 
 * @author hzjy
 * @date 2020-08-23
 */
@Service
public class AuditDelistServiceImpl implements AuditDelistService
{
    @Autowired
    private AuditDelistMapper auditDelistMapper;

    @Autowired
    private HzjyDelistMapper hzjyDelistMapper;

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    /**
     * 查询摘牌审核记录
     * 
     * @param auditId 摘牌审核记录ID
     * @return 摘牌审核记录
     */
    @Override
    public AuditDelist selectHzjyAuditDelistById(Integer auditId)
    {
        return auditDelistMapper.selectHzjyAuditDelistById(auditId);
    }

    /**
     * 查询摘牌审核记录列表
     * 
     * @param hzjyAuditDelist 摘牌审核记录
     * @return 摘牌审核记录
     */
    @Override
    public List<AuditDelist> selectHzjyAuditDelistList(AuditDelist hzjyAuditDelist)
    {
        return auditDelistMapper.selectHzjyAuditDelistList(hzjyAuditDelist);
    }

    /**
     * 新增摘牌审核记录
     * 
     * @param hzjyAuditDelist 摘牌审核记录
     * @return 结果
     */
    @Override
    public Long insertHzjyAuditDelist(AuditDelist hzjyAuditDelist)
    {
        hzjyAuditDelist.setCreateTime(DateUtils.getNowDate());
        return auditDelistMapper.insertHzjyAuditDelist(hzjyAuditDelist);
    }

    /**
     * 修改摘牌审核记录
     * 
     * @param hzjyAuditDelist 摘牌审核记录
     * @return 结果
     */
    @Override
    public int updateHzjyAuditDelist(AuditDelist hzjyAuditDelist)
    {
        return auditDelistMapper.updateHzjyAuditDelist(hzjyAuditDelist);
    }

    /**
     * 批量删除摘牌审核记录
     * 
     * @param auditIds 需要删除的摘牌审核记录ID
     * @return 结果
     */
    @Override
    public int deleteHzjyAuditDelistByIds(Integer[] auditIds)
    {
        return auditDelistMapper.deleteHzjyAuditDelistByIds(auditIds);
    }

    @Override
    public void agreeAuditDelis(AuditDelist auditDelist) {
        //插入审核记录
        auditDelist.setCreateTime(new Date());
        auditDelist.setEndState(3); //初审通过状态修改为3
        auditDelist.setType(1);  //
        auditDelistMapper.insertHzjyAuditDelist(auditDelist);
        //更新状态
        HzjyDelist hzjyDelist = new HzjyDelist();
        hzjyDelist.setAuditDelistId(auditDelist.getAuditId());
        hzjyDelist.setDelistId(auditDelist.getDelistId());
        hzjyDelist.setStatus(3); //初审通过状态为3
        hzjyDelistMapper.updateHzjyDelist(hzjyDelist);
    }

    @Override
    public void refuseAuditData(AuditDelist auditDelist) {
        //插入审核记录
        auditDelist.setCreateTime(new Date());
        auditDelist.setEndState(1); //驳回为待修改状态1,
        auditDelist.setType(0);  //驳回
        auditDelistMapper.insertHzjyAuditDelist(auditDelist);
        //更新状态
        HzjyDelist hzjyDelist = new HzjyDelist();
        hzjyDelist.setAuditDelistId(auditDelist.getAuditId());
        hzjyDelist.setDelistId(auditDelist.getDelistId());
        hzjyDelist.setStatus(1); //驳回为待修改状态1,
        hzjyDelistMapper.updateHzjyDelist(hzjyDelist);
    }

    @Override
    public void agreeAuditFinalDelis(AuditDelist auditDelist) {
        //插入审核记录
        auditDelist.setCreateTime(new Date());
        auditDelist.setEndState(4); //初审通过状态修改为3
        auditDelist.setType(1);  //
        auditDelistMapper.insertHzjyAuditDelist(auditDelist);
        //更新状态
        HzjyDelist hzjyDelist = new HzjyDelist();
        hzjyDelist.setAuditDelistId(auditDelist.getAuditId());
        hzjyDelist.setDelistId(auditDelist.getDelistId());
        hzjyDelist.setStatus(4); //初审通过状态为3
        hzjyDelistMapper.updateHzjyDelist(hzjyDelist);
    }

    @Override
    public void insertDelistNotice(HzjyDelistVO hzjyDelist) {
        List<AuditDelist> auditDelists = auditDelistMapper.selectAuditDelistByDelistId(hzjyDelist.getDelistId());
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType("1");
        sysNotice.setStatus("0");
        sysNotice.setCreateBy(SecurityUtils.getUsername());
        sysNotice.setUserId(hzjyDelist.getUserId());
        if (hzjyDelist.getStatus() == 3){
            sysNotice.setNoticeContent("恭喜您摘牌"+hzjyDelist.getProductName()+"初审通过");
            sysNotice.setNoticeTitle("审核通过");
        }else if (hzjyDelist.getStatus() == 4){
            sysNotice.setNoticeContent("恭喜您摘牌"+hzjyDelist.getProductName()+"终审通过");
            sysNotice.setNoticeTitle("审核通过");
        }else if (hzjyDelist.getStatus() == 1){
            sysNotice.setNoticeContent("您摘牌的"+hzjyDelist.getProductName()+"已被驳回。"+"\r\n"+"驳回原因："+auditDelists.get(0).getAuditExplain());
            sysNotice.setNoticeTitle("审核驳回");
        }
        sysNoticeMapper.insertNotice(sysNotice);
    }

}
